package com.sailorj.controller;

import com.sailorj.entity.TbPlan;
import com.sailorj.service.TbPlanService;
import com.sailorj.util.NumUtil;
import com.sailorj.util.PageBean;
import com.sailorj.util.SysLoging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/***
* @Description: 计划管理
* @Param:
* @return:
* @Author: huangshengjun
* @Date: 2020/4/19 19:28
*/
@CrossOrigin  //后端解决跨域方法注解
@Controller
@RequestMapping("/plan") //访问的路径
public class TbPlanController {
    private static final Logger logger = LoggerFactory.getLogger(TbPlanController.class);
    @Autowired
    private TbPlanService tbPlanService;


    /**
     * restful风格实现
     * @return
     */
    @ResponseBody
    @SysLoging("添加计划信息") //根据SysLoging的注解信息，进行方法的注释
    @RequestMapping(value = "/addPlan",method = RequestMethod.POST) //restful风格
    public String addPlan(TbPlan tbPlan){
        tbPlan.setPlanId(NumUtil.getNum(6));
        tbPlan.setIsDelete(0);
        tbPlan.setIsEnough(0);//人数是否已满,0-未满，1-已满
        tbPlan.setIsExamine(0);//是否审核,0-待审核，1-已通过，2-未通过
        tbPlan.setIsNum("0");//报名人数
        tbPlanService.addPlan(tbPlan);
        return "";
    }

    @ResponseBody
    @SysLoging("根据id查询计划信息")
    @RequestMapping(value = "/getPlanById",method = RequestMethod.GET)
    public TbPlan getPlanById(int id){
        return tbPlanService.getPlanById(id);
    }

    @ResponseBody
    @SysLoging("获取计划信息列表")
    @RequestMapping(value = "/getPlanList",method = RequestMethod.GET)
    PageBean getPlanList(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer size){
        return tbPlanService.getPlanList(page,size);
    }

    @ResponseBody
    @SysLoging("获取已经通过计划信息列表")
    @RequestMapping(value = "/getPlanListOne",method = RequestMethod.GET)
    PageBean getPlanListOne(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer size){
        return tbPlanService.getPlanListOne(page,size);
    }

    @ResponseBody
    @SysLoging("删除计划信息")
    @RequestMapping(value = "/deletePlan",method = RequestMethod.DELETE)
    public String deletePlan(int id) {
        TbPlan plan = tbPlanService.getPlanById(id);
        plan.setIsDelete(1);
        tbPlanService.updatePlanById(plan);
        return "";
    }

    @ResponseBody
    @SysLoging("更新计划信息")
    @RequestMapping(value = "/updatePlan",method = RequestMethod.PUT)
    public String updateUser(TbPlan tbPlan) {
        tbPlan.setIsDelete(0);
        tbPlanService.updatePlanById(tbPlan);
        return "";
    }


    @ResponseBody
    @SysLoging("通过计划申请信息")
    @RequestMapping(value = "/edit1",method = RequestMethod.PUT)
    public String edit1(int id) {
        TbPlan tbPlan =new TbPlan();
        TbPlan tbPlan1;
        tbPlan1 = tbPlanService.getPlanById(id);
        tbPlan.setId(tbPlan1.getId());
        tbPlan.setPlanId(tbPlan1.getPlanId());
        tbPlan.setPlanName(tbPlan1.getPlanName());
        tbPlan.setRequirement(tbPlan1.getRequirement());
        tbPlan.setNum(tbPlan1.getNum());
        tbPlan.setDepartment(tbPlan1.getDepartment());
        tbPlan.setUserName(tbPlan1.getUserName());
        tbPlan.setIsEnough(0);//人数是否已满,0-未满，1-已满
        tbPlan.setIsDelete(0);
        tbPlan.setIsNum("0");//报名人数
        tbPlan.setIsExamine(1);//是否审核,0-待审核，1-已通过，2-未通过
        tbPlanService.updatePlanById(tbPlan);
        logger.info(tbPlan.toString());
        return "";
    }

    @ResponseBody
    @SysLoging("拒绝申请计划信息")
    @RequestMapping(value = "/edit2",method = RequestMethod.PUT)
    public String edit2(int id) {
        TbPlan tbPlan = new TbPlan();
        TbPlan tbPlan1 = tbPlanService.getPlanById(id);
        tbPlan.setId(tbPlan1.getId());
        tbPlan.setPlanId(tbPlan1.getPlanId());
        tbPlan.setPlanName(tbPlan1.getPlanName());
        tbPlan.setRequirement(tbPlan1.getRequirement());
        tbPlan.setNum(tbPlan1.getNum());
        tbPlan.setDepartment(tbPlan1.getDepartment());
        tbPlan.setUserName(tbPlan1.getUserName());
        tbPlan.setIsEnough(0);//人数是否已满,0-未满，1-已满
        tbPlan.setIsDelete(0);
        tbPlan.setIsNum("0");//报名人数
        tbPlan.setIsExamine(2);//是否审核,0-待审核，1-已通过，2-未通过
        tbPlanService.updatePlanById(tbPlan);
        logger.info(tbPlan.toString());
        return "";
    }

    @ResponseBody
    @SysLoging("计划信息人数加1")
    @RequestMapping(value = "/edit3",method = RequestMethod.PUT)
    public String edit3(int id) {
        TbPlan tbPlan = new TbPlan();
        TbPlan tbPlan1 = tbPlanService.getPlanById(id);
        tbPlan.setId(tbPlan1.getId());
        tbPlan.setPlanId(tbPlan1.getPlanId());
        tbPlan.setPlanName(tbPlan1.getPlanName());
        tbPlan.setRequirement(tbPlan1.getRequirement());
        tbPlan.setNum(tbPlan1.getNum());
        tbPlan.setDepartment(tbPlan1.getDepartment());
        tbPlan.setUserName(tbPlan1.getUserName());

        tbPlan.setIsDelete(0);

        tbPlan.setIsExamine(tbPlan1.getIsExamine());//是否审核,0-待审核，1-已通过，2-未通过
        try{
        int a = Integer.valueOf(tbPlan.getNum()).intValue();//设定招聘的人数
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        int num = 0;
        try{
             num = Integer.valueOf(tbPlan1.getIsNum()).intValue();//设定招聘的人数
            logger.info("第一个num=="+num);
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        num = num +1;
        logger.info("第2个num=="+num);
        tbPlan.setIsNum(String.valueOf(num));//报名人数

        try{
            int b = Integer.valueOf(tbPlan.getIsNum()).intValue();//报名人数
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        if(tbPlan.getNum().equals(tbPlan.getIsNum())){
            tbPlan.setIsEnough(1);//人数是否已满,0-未满，1-已满
        }else {
            tbPlan.setIsEnough(0);//人数是否已满,0-未满，1-已满
        }
        tbPlanService.updatePlanById(tbPlan);
        logger.info(tbPlan.toString());
        return "";
    }


}
